package com.hbpa.util;

import java.sql.*;
import java.io.*;
import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class JdbcConnectionFactory
{
    private static Properties config = new Properties();
    
    static{
        InputStream in = ConnectionFactory.class.getResourceAsStream("db-config.properties.mysql");
        if(in == null){
            throw new ExceptionInInitializerError("no file:db-config.properties found error!");
        }        
        
        try{
            config.load(in);
            in.close();
        }catch(IOException e){
            e.printStackTrace();
            throw new ExceptionInInitializerError("failed to load file!");
        }
    }
     
	public static Connection getConnection() throws SQLException
    {  
        try{
            String driverClassName = config.getProperty("driverClassName");
            String jdbcURL = config.getProperty("jdbcURL");
            String userName = config.getProperty("userName");
            String password = config.getProperty("password");
            Class.forName(driverClassName);
            return DriverManager.getConnection(jdbcURL, userName, password);
        }catch(ClassNotFoundException e){
            e.printStackTrace();
            throw new SQLException(e.getMessage());
        } 
    }
}